import { _decorator, Component, Node, UITransform, Vec3, view, Vec2, director } from 'cc';
import { BoardManager } from '../core/BoardManager';
import { GameManager } from '../core/GameManager';
import { Tile } from './Tile';

const { ccclass, property } = _decorator;

/**
 * 棋盘组件
 * 管理棋盘的显示和布局
 */
@ccclass('Board')
export class Board extends Component {
    @property(UITransform)
    private boardTransform: UITransform = null;
    
    @property({type: Node, tooltip: "棋盘背景节点"})
    public boardBackground: Node = null;
    
    @property({type: Node, tooltip: "方块容器节点"})
    public tilesContainer: Node = null;
    
    @property({type: Number, tooltip: "棋盘边距"})
    public boardPadding: number = 10;
    
    @property(BoardManager)
    private boardManager: BoardManager = null;
    
    @property(GameManager)
    private gameManager: GameManager = null;
    
    // 生命周期函数
    onLoad() {
        if (!this.boardTransform) {
            this.boardTransform = this.getComponent(UITransform);
        }
        
        this.findManagers();
    }
    
    /**
     * 查找管理器实例
     */
    private findManagers(): void {
        // 查找BoardManager
        if (!this.boardManager) {
            const boardManagerNode = director.getScene().getChildByName('BoardManager');
            if (boardManagerNode) {
                this.boardManager = boardManagerNode.getComponent(BoardManager);
            }
            
            if (!this.boardManager) {
                console.warn('未找到BoardManager引用，棋盘功能可能无法正常工作');
            }
        }
        
        // 查找GameManager
        if (!this.gameManager) {
            const gameManagerNode = director.getScene().getChildByName('GameManager');
            if (gameManagerNode) {
                this.gameManager = gameManagerNode.getComponent(GameManager);
            }
            
            if (!this.gameManager) {
                console.warn('未找到GameManager引用，某些功能可能无法正常工作');
            }
        }
    }
    
    start() {
        // 初始化棋盘大小
        this.setupBoardSize();
        
        // 设置棋盘管理器引用
        if (this.boardManager) {
            this.boardManager.setTilesContainer(this.tilesContainer);
            
            // 初始化棋盘
            this.boardManager.initBoard();
        }
    }
    
    /**
     * 设置棋盘大小
     */
    private setupBoardSize(): void {
        if (!this.boardTransform || !this.tilesContainer || !this.boardBackground) {
            console.error("缺少必要的组件引用");
            return;
        }
        
        // 获取游戏管理器中的棋盘大小
        let boardSize = 8;
        if (this.gameManager) {
            boardSize = this.gameManager.boardSize;
        }
        
        // 获取方块大小和间距
        let tileSize = 60;
        let tileGap = 5;
        if (this.boardManager) {
            tileSize = this.boardManager.getTileSize();
            tileGap = this.boardManager.getTileGap();
        }
        
        // 计算棋盘总宽度和高度
        const totalSize = boardSize * tileSize + (boardSize - 1) * tileGap + this.boardPadding * 2;
        
        // 设置棋盘大小
        this.boardTransform.width = totalSize;
        this.boardTransform.height = totalSize;
        
        // 设置背景大小
        const bgTransform = this.boardBackground.getComponent(UITransform);
        if (bgTransform) {
            bgTransform.width = totalSize;
            bgTransform.height = totalSize;
        }
        
        // 调整方块容器位置，使其在棋盘中居中
        this.tilesContainer.setPosition(new Vec3(this.boardPadding, -this.boardPadding, 0));
        
        // 适配屏幕大小
        this.adaptToScreen();
    }
    
    /**
     * 适配屏幕大小
     */
    private adaptToScreen(): void {
        // 获取屏幕尺寸
        const screenSize = view.getVisibleSize();
        
        // 计算缩放比例，确保棋盘不会超出屏幕
        const maxScale = Math.min(
            (screenSize.width * 0.9) / this.boardTransform.width,
            (screenSize.height * 0.7) / this.boardTransform.height
        );
        
        // 应用缩放
        this.node.scale = new Vec3(maxScale, maxScale, 1);
    }
    
    /**
     * 重置棋盘
     */
    public resetBoard(): void {
        if (this.boardManager) {
            this.boardManager.initBoard();
        }
    }
} 